/*
 *  (C) Copyright 2008 Insula T.I. Ltda.
 * 
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.core;

import java.util.LinkedList;
import java.util.List;

import org.openinsula.vulcano.core.command.invoker.LocalCommandInvoker;
import org.openinsula.vulcano.core.command.processor.CommandProcessor;
import org.openinsula.vulcano.core.command.processor.DependencyInjectionCommandProcessor;
import org.openinsula.vulcano.core.command.processor.TransactionalCommandProcessor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.wiring.BeanConfigurerSupport;
import org.springframework.beans.factory.wiring.BeanWiringInfo;
import org.springframework.beans.factory.wiring.BeanWiringInfoResolver;
import org.springframework.beans.factory.wiring.ClassNameBeanWiringInfoResolver;

public class LocalCommandInvokerFactoryBean implements FactoryBean, BeanFactoryAware {

	private final BeanConfigurerSupport beanConfigurerSupport = new BeanConfigurerSupport();

	private boolean useDependencyInjection = true;

	private boolean manageTransactions = true;

	private List<CommandProcessor> customCommandProcessorList;

	@Override
	public Object getObject() throws Exception {
		LocalCommandInvoker commandInvoker = new LocalCommandInvoker();

		LinkedList<CommandProcessor> commandProcessorList = new LinkedList<CommandProcessor>();
		addCustomCommandProcessors(commandProcessorList);
		addDefaultCommandProcessors(commandProcessorList);

		commandInvoker.setCommandProcessorList(commandProcessorList);
		
		return commandInvoker;
	}

	private void addDefaultCommandProcessors(final LinkedList<CommandProcessor> commandProcessorList) {
		if (useDependencyInjection) {
			CommandProcessor commandProcessor = new DependencyInjectionCommandProcessor();
			beanConfigurerSupport.configureBean(commandProcessor);
			commandProcessorList.addFirst(commandProcessor);
		}

		if (manageTransactions) {
			CommandProcessor commandProcessor = new TransactionalCommandProcessor();
			beanConfigurerSupport.configureBean(commandProcessor);
			commandProcessorList.addLast(commandProcessor);
		}
	}

	private void addCustomCommandProcessors(final LinkedList<CommandProcessor> commandProcessorList) {
		if (customCommandProcessorList != null && !customCommandProcessorList.isEmpty()) {
			commandProcessorList.addAll(customCommandProcessorList);
		}
	}

	@Override
	public Class<?> getObjectType() {
		return LocalCommandInvoker.class;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}

	public void setUseDependencyInjection(final boolean useDependencyInjection) {
		this.useDependencyInjection = useDependencyInjection;
	}

	public void setManageTransactions(final boolean manageTransactions) {
		this.manageTransactions = manageTransactions;
	}

	public void setCustomCommandProcessorList(final List<CommandProcessor> customCommandProcessorList) {
		this.customCommandProcessorList = customCommandProcessorList;
	}

	@Override
	public void setBeanFactory(final BeanFactory beanFactory) throws BeansException {
		BeanWiringInfoResolver beanWiringInfoResolver = new ClassNameBeanWiringInfoResolver() {

			@Override
			public BeanWiringInfo resolveWiringInfo(Object beanInstance) {
				return new BeanWiringInfo(BeanWiringInfo.AUTOWIRE_BY_NAME, true);
			}

		};

		beanConfigurerSupport.setBeanWiringInfoResolver(beanWiringInfoResolver);
		beanConfigurerSupport.setBeanFactory(beanFactory);
	}

}
